package com.tz.movie.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.movie.domain.User;
import com.tz.movie.domain.UserExample;
import com.tz.movie.domain.status;
import com.tz.movie.mapper.UserMapper;

import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired
  UserMapper userMapperImpl;

  @PostMapping(value = "/getAllUser")
  public String getAllUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    // if (session.getAttribute("userbase") == null) {
    //   session.setAttribute("userbase", null);
    //   return result.toJSONString();
    // }
    // JSONObject jsonObject = JSONObject.parseObject((String) session.getAttribute("userbase"));
    // if (jsonObject == null || !jsonObject.get("groupid").equals(1)) {
    //   result.put("result", status.s_error);
    //   result.put("groupid", jsonObject.get("groupid"));

    //   return result.toJSONString();
    // }
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andGroupidEqualTo(2);
    List<User> users = userMapperImpl.selectByExample(userExample);
    JSONArray jsonArray = new JSONArray();
    if (users.size() == 0) {
      result.put("result", status.s_ok);
      result.put("data", users.get(0));
      return result.toJSONString();
    }
    for (User user : users) {
      JSONObject obj = JSONObject.parseObject(user.toString());
      jsonArray.add(obj);
    }
    result.put("result", status.s_ok);

    result.put("data", jsonArray);
    return result.toJSONString();
  }


  
  @PostMapping(value = "/updateUser")
  public String updateUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    int id = jsonparm.getInteger("id");

    String name = jsonparm.getString("name");
    String password = jsonparm.getString("password");
    String realname = jsonparm.getString("realname");
    String phone = jsonparm.getString("phone");
    String address = jsonparm.getString("address");
    User user = new User ();
    user.setName(name);
    user.setPassword(password);
    user.setRealname(realname);
    user.setPhone(phone);
    user.setAddress(address);
    user.setBalance(0d);
    user.setGroupid(2);



    UserExample userExample= new UserExample();
    UserExample.Criteria criteria= userExample.createCriteria();
    criteria.andIdEqualTo(id);
    userMapperImpl.updateByExampleSelective(user, userExample);

    result.put("result", status.s_ok);
    return result.toJSONString();
  }


  
  @PostMapping(value = "/regUser")
  public String regUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    String name = jsonparm.getString("name");
    String password = jsonparm.getString("password");
    String realname = jsonparm.getString("realname");
    String phone = jsonparm.getString("phone");
    String address = jsonparm.getString("address");
    User user = new User ();
    user.setName(name);
    user.setPassword(password);
    user.setRealname(realname);
    user.setPhone(phone);
    user.setAddress(address);
    user.setBalance(0d);
    user.setGroupid(2);

    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andNameEqualTo(name);
    List<User> users = userMapperImpl.selectByExample(userExample);
    if(users.size() > 0 ){
      result.put("result", status.s_error);
      return result.toJSONString();
    }
    userMapperImpl.insert(user);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

  @PostMapping(value = "/delUser")
  public String delUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    // TODO: process POST request
    JSONObject result = new JSONObject();
    int delid = jsonparm.getInteger("id");
    UserExample userExample= new UserExample();
    UserExample.Criteria criteria =userExample.createCriteria();
    criteria.andIdEqualTo(delid);
    userMapperImpl.deleteByExample(userExample);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

}